package com.demo.datastructure.array;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description: letcode 15 三数之和
 * @author: ljr
 * @time: 2021/5/2 17:53
 * @since 1.0
 */
public class ThreeSumArray {

    public List<List<Integer>> threeSum(int[] nums) {
        int length = nums.length;
        if(length < 3){
             return new ArrayList();
        }
        List<List<Integer>> list = new ArrayList<>();
        if(length == 3){
            if(nums[0]+nums[1]+nums[2] == 0){
                List<Integer> sonList = new ArrayList<>();
                sonList.add(nums[0]);
                sonList.add(nums[1]);
                sonList.add(nums[2]);
                list.add(sonList);
                return list;
            }
            return new ArrayList();
        }



        Set<Integer> set = new HashSet<>();
        for(int c = 0; c < length-2; c++) {
            int num1 = nums[c];
            int i = c+1;
            if(c != 0 && set.contains(num1)){
                continue;
            }
            boolean flag = false;
            Set<Integer> setSon = new HashSet<>();
            for (; i < length - 1; i++) {
                int num2 = nums[i];
                if (setSon.contains(num2)) {
                    continue;
                }

                if(c != 0 && set.contains(num2)){
                    continue;
                }
                int k = i + 1;
                for (; k < length; k++) {
                    int num3 = nums[k];
                    if(c != 0 && set.contains(num3)){
                        continue;
                    }

                    if (num1 + num2 + num3 == 0) {
                        List<Integer> sonList = new ArrayList<>();
                        sonList.add(num1);
                        sonList.add(num2);
                        sonList.add(num3);
                        list.add(sonList);

                        setSon.add(num2);
                        setSon.add(num3);
                        flag = true;
                        break;
                    }
                }
            }

            if(flag){
                set.add(num1);
            }
        }
        return list;
    }


    public static void main(String[] args) {
        ThreeSumArray threeSumArray = new ThreeSumArray();
        int[] nums = new int[]{-4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0};
        List<List<Integer>> list= threeSumArray.threeSum(nums);
        System.out.println("1");
    }
}
